package cn.com.czpb.client.module.news.view;

import android.app.Activity;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import java.util.List;

import cn.com.czpb.client.R;
import cn.com.czpb.client.model.news.ChildsBean;
import cn.com.czpb.client.module.news.adapter.NewsColumsViewAdapter;
import cn.com.czpb.client.utils.PageCtrl;

/**
 * Created by 猪猪侠 on 2017/7/25.
 */

public class TopViewNewsColumns {
    private View rootView;
    private Activity mActivity;
    private Handler viewHandler = new Handler();

    private HorizontalListView horizontalListView;
    private ImageView topnanningmore;
    private List<ChildsBean> list;
    private NewsColumsViewAdapter topadapter;

    private ColumsItemClick columsItemClick;
    public static TopViewNewsColumns instance(Activity activity, List<ChildsBean> list,ColumsItemClick columsItemClick){

        return new TopViewNewsColumns(activity,list,columsItemClick);
    }

    public TopViewNewsColumns(Activity activity, List<ChildsBean> list,ColumsItemClick columsItemClick){
        //
        this.columsItemClick = columsItemClick;
        this.list = list;
        this.mActivity = activity;
        initView(activity);
    }

    public View getView(){
        return rootView;
    }
    /**
     * @param context
     */
    private void initView(Activity context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        rootView = layoutInflater.inflate(R.layout.view_nanning,null);
        horizontalListView = rootView.findViewById(R.id.city_horizontallv);
        topnanningmore = rootView.findViewById(R.id.top_view_nanning_more);
        topadapter = new NewsColumsViewAdapter(mActivity,list);
        horizontalListView.setAdapter(topadapter);
        horizontalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ChildsBean bean = list.get(position);
                if ("1".equals(bean.getType())){
                    columsItemClick.onItemClick(bean.getTidX());
                }else{
                    PageCtrl.start2FuWuDetialActivity(mActivity,list.get(position).getLink());
                }
            }
        });
        viewHandler.removeCallbacksAndMessages(null);
    }
    public void refreshData(List<ChildsBean> list){
        this.list.clear();
        this.list.addAll(list);
        topadapter.notifyDataSetChanged();
    }

    public interface ColumsItemClick{
        void onItemClick(String tid);
    }
}
